﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PhotoHsn.BLL.Adapters;
using Bxas.Core.Exceptions;
using System.Diagnostics;
using PhotoHsn.BLL.Core;

namespace PhotoHsn.BLL.Authantication
{
    public class MembersBs : PhotoHsn.BLL.Abstracts._MembersBs
    {
        public string get_fullname(Members p_member)
        {
            if (string.IsNullOrEmpty(p_member.FirstName) && string.IsNullOrEmpty(p_member.LastName)) return string.Empty;

            return p_member.FirstName + (string.IsNullOrEmpty(p_member.MiddleName) ? "" : (" " + p_member.MiddleName)) + " " + p_member.LastName;
        }

        public int get_for_login(string p_userNameOrEMail, string p_password, ref Members p_member)
        {
            try
            {
                System.Data.SqlClient.SqlCommand sqlcommand = new System.Data.SqlClient.SqlCommand();
                sqlcommand.CommandText = SqlStrrings.MemberSqlStrings.LoginSql;

                sqlcommand.Parameters.AddWithValue(Members.ColumnNames.Password, p_password);
                sqlcommand.Parameters.AddWithValue(Members.ColumnNames.Email, p_userNameOrEMail);
                sqlcommand.Parameters.AddWithValue(Members.ColumnNames.UserName, p_userNameOrEMail);

                List<Members> list = get(sqlcommand);
                if (list == null)
                    throw new BxasCoreException("Kullanıcı bilgileri yanlış.") { HataTuru = EnumaHataTuru.web };
                p_member = list.First<Members>();

                switch (p_member.IsStatus)
                {
                    case EnumMemberStatus.Passive:
                        throw new BxasCoreException("Kullanıcı aktif değil.") { HataTuru = EnumaHataTuru.web };

                    case EnumMemberStatus.NoMember:
                        throw new BxasCoreException("Kullanıcı bilgileri yanlış") { HataTuru = EnumaHataTuru.web };

                    case EnumMemberStatus.DeniedaMonth:
                        throw new BxasCoreException("Kullanıcı bir ay engellendi") { HataTuru = EnumaHataTuru.web };

                    case EnumMemberStatus.DeniedaYear:
                        throw new BxasCoreException("Kullanıcı bir yıl engellendi") { HataTuru = EnumaHataTuru.web };

                    default:
                        break;
                }
                return p_member.IsStatus;
            }
            catch (BxasCoreException ex)
            {
                throw ex;// ErrorLogInsert(ex, this, new StackFrame());
            }


        }

        public bool get_for_UserNameControl(string p_userName)
        {
            System.Data.SqlClient.SqlCommand sqlcommand = new System.Data.SqlClient.SqlCommand();
            sqlcommand.CommandText = SqlStrrings.MemberSqlStrings.UserNameControlSql;
            sqlcommand.Parameters.AddWithValue(Members.ColumnNames.UserName, p_userName);
            try
            {
                List<Members> list = get(sqlcommand);
                if (list == null) return false;
                return list.Count > 0;
            }
            catch (BxasCoreException ex)
            {
                throw ex;
            }

        }
        public bool get_for_EmailControl(string p_email)
        {
            System.Data.SqlClient.SqlCommand sqlcommand = new System.Data.SqlClient.SqlCommand();
            sqlcommand.CommandText = SqlStrrings.MemberSqlStrings.EmailControlSql;
            sqlcommand.Parameters.AddWithValue(Members.ColumnNames.Email, p_email);
            try
            {
                List<Members> list = get(sqlcommand);
                if (list == null) return false;
                return list.Count > 0;
            }
            catch (BxasCoreException ex)
            {
                throw ex;// ErrorLogInsert(ex, this, new StackFrame());
            }

        }

        public Members get_for_ForgotPassword(string p_Email)
        {
            System.Data.SqlClient.SqlCommand sqlcommand = new System.Data.SqlClient.SqlCommand();
            sqlcommand.CommandText = SqlStrrings.MemberSqlStrings.ForgtotPasswordSql;
            sqlcommand.Parameters.AddWithValue(Members.ColumnNames.Email, p_Email);
            try
            {
                List<Members> list = get(sqlcommand);
                if (list == null) return null;
                return list.First<Members>();
            }
            catch (BxasCoreException ex)
            {
                throw ex;// ErrorLogInsert(ex, this, new StackFrame());
            }

        }




    }
}